import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.util.ArrayList;

class Player extends Entity
{
	private boolean moves[];
	private int mx;
	private int my;
	private boolean isFiring;
	private double accuracy;
	private Weapon gun;
	
	public Player(double x, double y)
	{
		moves = new boolean[4];
		
		setMaxHealth(100);
		heal(100);
		
		this.x = x;
		this.y = y;
		
		speed = 2;
		
		accuracy = 0;
		
		gun = new Pistol();
		
		loadImage("images/player.rwe");
	}
	
	public void damage(int d)
	{
		health -= d;
		
		if(health < 0)
			mark();
	}
	
	public void collide(Entity e)
	{
		e.damage(0);
	}
	
	public void setMoveUp(boolean m){moves[0] = m;}
	public void setMoveRight(boolean m){moves[1] = m;}
	public void setMoveDown(boolean m){moves[2] = m;}
	public void setMoveLeft(boolean m){moves[3] = m;}
	public void setMouse(int mx, int my){this.mx = mx; this.my = my;}
	
	public void move()
	{
		calculateAngle();
		
		if(moves[0])
			y -= speed;
		if(moves[1])
			x += speed;
		if(moves[2])
			y += speed;
		if(moves[3])
			x -= speed;
	}
	
	private void calculateAngle()
	{
		if(mx == x)
			if(my > y)
				setAngle(Math.PI/2);
			else
				setAngle(Math.PI/2*3);
		else
			if(mx > x)
				setAngle(Math.atan((my-y)/(mx-x)));
			else
				setAngle(Math.atan((my-y)/(mx-x))+Math.PI);
	}
	
	public void recalculateBoundingBox()
	{
		AffineTransform af = new AffineTransform();
		af.rotate(angle);
		
		int tx[] = new int[xloc.length];
		int ty[] = new int[yloc.length];
		
		for(int a = 0; a < xloc.length; a++)
		{
			Point2D.Double d = (Point2D.Double)af.transform(new Point2D.Double(xloc[a]-image.getWidth(null)/2,yloc[a]-image.getHeight(null)/2),null);
			tx[a] = (int)(d.x)+(int)x;
			ty[a] = (int)(d.y)+(int)y;
		}
		
		boundingBox = new java.awt.Polygon(tx,ty,xloc.length);
	}
	
	public double getAccuracy()
	{
		return gun.getAccuracy();
	}
	
	public void fire(ArrayList<Entity> ents)
	{
		gun.fire(ents,x,y,angle);
	}
	
	public void setFire(boolean b)
	{
		isFiring = b;
	}
	
	public boolean isFiring()
	{
		return isFiring;
	}
}